package com.kaifamiao.month04.day26.work;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SizeAndFind {

    public ArrayList<File> list = new ArrayList<>();
    static ArrayList<File> ph = new ArrayList<>();
    public File file;

    public SizeAndFind(File file) {
        this.file = file;
    }
    //找到所有目录和文件
    public void allFile() {
        list.add(this.file);
        ArrayList<File> allfile = new ArrayList<>();
        for(int i = 0; i < list.size();i++) {
            File[] t = list.get(i).listFiles();
            if(t == null) {
                allfile.add(list.get(i));
                String str = list.get(i).toString();
                //在查询的时候就分辨是不是图片
                if(str.endsWith(".png") || str.endsWith(".bmp")
                        || str.endsWith(".jpg") || str.endsWith(".jpeg")
                        || str.endsWith(".jpe") || str.endsWith(".jfif")
                        || str.endsWith(".gif") || str.endsWith(".tif")
                        || str.endsWith(".tiff") || str.endsWith(".heic")) {
                    ph.add(list.get(i));
                }
                continue;
            }
            List<File> temp = List.of(t);
            list.addAll(temp);
        }
        list = allfile;
    }
    //文件大小
    public double fileSize(){
        double length = 0;
        Iterator<File> it = list.iterator();
        while(it.hasNext()) {
            length += it.next().length();
        }
        return length / 1024;
    }
    //图片
    public ArrayList<File> photo(){
        return ph;
    }
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        SizeAndFind saf = new SizeAndFind(new File("E:\\"));
        saf.allFile();
        Iterator<File> it = saf.photo().iterator();
        while(it.hasNext()) {
            System.out.println(it.next());
        }
        System.out.println(ph.size());
        System.out.println(System.currentTimeMillis() - start);
    }
}
